<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>state练习-简易轮播图</title>
    <style>
        body{
            width: 100%;
            height:100%;
            background: radial-gradient(#eee, rgb(66, 160, 184));
        }
        *{
            margin:0;
            padding:0;
            user-select: none;
        }
        .container{
            width: 480px;
            height: 270px;
            margin: 150px auto;
            position:relative;
        }   
        .btn{
            position:absolute;
            width: 50px;
            height: 50px;
            border:solid 1px #000;
            border-radius: 50%;
            text-align:center;
            line-height: 45px;
            font-size: 40px;
            top: 100px;
            background: white;
            cursor: pointer;
        }

        .left{
            left: 0;
        }
        .right{
            right:0;
        }


    </style>
</head>
<body>
    <div id="root"> </div>
    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        //声明一个类
        class App extends React.Component{
            state = {
                //所有图片的 url
                images: [
                    './images/1.jpg',
                    './images/2.jpg',
                    './images/3.jpg',
                    './images/4.jpg',
                    './images/5.jpg',
                ],
                //当前正在显示的图片的『下标』
                index: 0
            }

            render(){
                //获取当前图片的 url
                let {images, index} = this.state;
                let url = images[index];
                return <div className="container">
                        <img width="100%" src={url} alt=""/>
                        <div onClick={this.prev} className="left btn">&lt;</div>
                        <div onClick={this.next} className="right btn">&gt;</div>
                    </div>
            }

            //下一章轮播图
            next = () => {
                //获取 index
                let {index, images} = this.state;
                //判断 index 的值
                if(index >= images.length-1){
                    index=0
                }else{
                    index = index+1;
                }
                this.setState({
                    index: index
                })
            }       

            //声明事件的方法
            prev = () =>{
                //在原来 index 的基础上 +1
                let {index, images} = this.state;
                if(index <= 0){
                    index = images.length-1;
                }else{
                    index = index-1;
                }
                this.setState({
                    index: index
                })
            }

           
        }

        ReactDOM.render(<App />, document.querySelector("#root"));
    </script>
</body>
</html>